/*****************************************************************************************
   
   Copyright (c) Microsoft Corporation. All rights reserved.

   Use of this code sample is subject to the terms of the Microsoft
   Public License, a copy of which should always be distributed with
   this file.  You can also access a copy of this license agreement at:
   http://www.microsoft.com/opensource/licenses.mspx#Ms-PL

 ****************************************************************************************/
using System;

namespace Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrary
{
    public class PickList
    {
        #region private variables

        private System.Xml.XmlElement m_XmlElement;

        private string m_ID;

        private Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrary.Binding m_Binding;

        private System.Collections.Generic.SortedList<string, Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrary.PickListItem> m_PickListItemSortedList = new System.Collections.Generic.SortedList<string, Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrary.PickListItem>();

        #endregion

        public PickList(Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrary.Binding i_Binding, string i_ID)
        {
            this.PickListInitialize(i_ID);

            string ElementName = Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrary.Constants.PICKLIST_ELEMENT_NAME;
            if (!System.String.IsNullOrEmpty(i_Binding.NamespacePrefix))
                ElementName = i_Binding.NamespacePrefix + ":" + ElementName;

            m_XmlElement = i_Binding.XmlElement.OwnerDocument.CreateElement
                (
                ElementName,
                Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrary.Constants.SIMPLE_FEED_BINDINGS_XML_NAMESPACE_URI
                );

            m_XmlElement.SetAttribute(Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrary.Constants.ID_ATTRIBUTE, m_ID);
        }

        public PickList(Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrary.Binding i_Binding, System.Xml.XmlElement i_PickListXmlElement)
        {
            if (i_Binding == null)
                throw new System.ArgumentException("i_Binding must be non-null!");

            if (i_PickListXmlElement == null)
                throw new System.ArgumentException("i_PickListXmlElement must be non-null!");

            bool InvalidElement =
                (i_PickListXmlElement.LocalName != Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrary.Constants.PICKLIST_ELEMENT_NAME) ||
                (i_PickListXmlElement.NamespaceURI != Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrary.Constants.SIMPLE_FEED_BINDINGS_XML_NAMESPACE_URI);

            if (InvalidElement)
                throw new System.ArgumentException("Invalid i_PickListXmlElement!");

            m_XmlElement = i_PickListXmlElement;

            string ID = m_XmlElement.GetAttribute(Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrary.Constants.ID_ATTRIBUTE);

            this.PickListInitialize(ID);

            System.Xml.XmlNodeList PickListItemXmlNodeList = i_PickListXmlElement.SelectNodes
                (
                i_Binding.NamespacePrefix + ":" + Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrary.Constants.PICKLIST_ITEM_ELEMENT_NAME,
                i_Binding.XmlNamespaceManager
                );

            foreach (System.Xml.XmlElement PickListItemXmlElement in PickListItemXmlNodeList)
            {
                Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrary.PickListItem PickListItem = new Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrary.PickListItem
                    (
                    this,
                    PickListItemXmlElement
                    );

                this.AddPickListItem(PickListItem, false);
            }
        }

        internal void PickListInitialize(string i_ID)
        {
            if (System.String.IsNullOrEmpty(i_ID))
                throw new System.ArgumentNullException(i_ID);

            m_ID = i_ID;
        }

        public Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrary.Binding Binding
        {
            get
            {
                return m_Binding;
            }

            internal set
            {
                if (value == null)
                    throw new System.ArgumentException("Value must be non-null!");

                m_Binding = value;
            }
        }

        public string ID
        {
            get
            {
                return m_ID;
            }
        }


        public System.Xml.XmlElement XmlElement
        {
            get
            {
                return m_XmlElement;
            }
        }

        public bool HasPickListItem(string i_Value)
        {
            return m_PickListItemSortedList.ContainsKey(i_Value);
        }

        public bool FindPickListItem(string i_Value, out Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrary.PickListItem o_PickListItem)
        {
            o_PickListItem = null;

            if (!this.HasPickListItem(i_Value))
                return false;

            o_PickListItem = m_PickListItemSortedList[i_Value];

            return true;
        }

        public Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrary.PickListItem GetPickListItem(string i_Value)
        {
            Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrary.PickListItem PickListItem = null;

            if (!this.FindPickListItem(i_Value, out PickListItem))
                throw new System.ArgumentException("Invalid value: " + i_Value);

            return PickListItem;
        }

        public System.Collections.Generic.IList<Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrary.PickListItem> PickListItems
        {
            get
            {
                return m_PickListItemSortedList.Values;
            }
        }

        public Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrary.PickListItem CreatePickListItemFromXmlElement(System.Xml.XmlElement i_PickListItemElement)
        {
            Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrary.PickListItem PickListItem = new Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrary.PickListItem
                (
                this,
                i_PickListItemElement
                );

            return PickListItem;
        }

        public Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrary.PickListItem CreatePickListItem(string i_ID, string i_Value)
        {
            Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrary.PickListItem PickListItem = new Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrary.PickListItem
                (
                this,
                i_ID,
                i_Value
                );

            return PickListItem;
        }
        
        public void AddPickListItem(Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrary.PickListItem i_PickListItem)
        {
            this.AddPickListItem
                (
                i_PickListItem,
                true
                );
        }

        private void AddPickListItem(Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrary.PickListItem i_PickListItem, bool i_AppendXmlElement)
        {
            if (this.HasPickListItem(i_PickListItem.Value))
                throw new System.ArgumentException("PickListItem with same value already exists in Binding!");

            m_PickListItemSortedList.Add(i_PickListItem.Value, i_PickListItem);

            if (i_AppendXmlElement)
                m_XmlElement.AppendChild(i_PickListItem.XmlElement);
        }
    }
}
